#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>

int main(int argc, char const *argv[])
{
    int fd = open("/dev/my_sample_chardev", O_RDWR);
    if (fd < 0)
    {
        perror("打开文件失败");
        return fd;
    }
    char buff[10] = {'h', 'e', 'l', 'l', 'o', ',', 'w', 'o', 'l', 'd'};
    memset(buff, 0, sizeof(buff));
    printf("%d, %s,%s\n", argc, argv[0], argv[1]);
    if (argc == 2)
    {
        int length = 0;
        while (*(argv[1] + length) != '\0')
        {
            printf("%c", *(argv[1] + length));
            length++;
            // if(length > 15) {
            //     length = 10;
            //     break;
            // }
        }
        printf("\n");
        // length = sizeof(argv[1])> sizeof(buff)? sizeof(buff) : sizeof(argv[1]);
        memcpy(buff, argv[1], length > sizeof(buff) ? sizeof(buff) : length);
        printf("wirte test is %s, length is %d\n", buff, length);
    }
    size_t count = write(fd, &buff, sizeof(buff));
    if (count < 0)
    {
        perror("write error");
        return count;
    }
    memset(buff, 0, sizeof(buff));
    count = read(fd, &buff, sizeof(buff));
    if (count < 0)
    {
        perror("read error");
        return count;
    }

    printf("you read %s\n", buff);

    // for (int i = 0; i < count; i++)
    // {
    //     /* code */
    //     printf("%d is %c\n", i, buff[i]);
    // }

    close(fd);

    return 0;
}
